<?php

/**
 * TYPE: 1=>push item; 2=>relation; 3=>comment
 * STATUS: 0=>old; 1=>new
 * 	
 * @author jiapeng
 *
 */
class Notifications_Model extends Basic_Das {
	const PUSH_ITEM_TYPE = 1;
	const RELATION_TYPE = 2;
	const COMMENT_TYPE = 3;
	const READ_STATUS = 0;
	const NEW_STATUS = 1;
	
	public function getNotifications( $user_id, $type, $status, $current_num, $next_num ){
		$sql_where = " WHERE USERID=\"$user_id\" ";
		if($type !== NULL)
			$sql_where .= " AND TYPE=$type ";
		if($status !== NULL)
			$sql_where .= " AND STATUS=$status ";
			
		$sql = "SELECT
					NOTIFICATIONID,
					TYPE,
					USERID,
					CONTENT,
					STATUS,
					CREATETIME
				FROM NOTIFICATIONS
				$sql_where
				ORDER BY
					CREATETIME DESC
				LIMIT
					$current_num, $next_num;";
		print_r($sql);
		
		$this->load->database();
		$query = $this->db->query($sql);
		$this->db->close();
		
		$json = json_encode($query->result());
		print_r($json);
		return $json;
	}
	
	private function addNotification( $user_id, $type, $content ) {
		$status = self::NEW_STATUS;
		$sql = "INSERT INTO NOTIFICATIONS (
					USERID,
					TYPE,
					CONTENT,
					STATUS,
					CREATETIME )
				VALUES (
					'$user_id',
					$type,
					'$content',
					$status,
					now());";
		print_r($sql);
		
		$this->load->database();
		$query = $this->db->query($sql);
		$this->db->close();
	}
	
	public function getNewPushItemNotifications( $user_id, $current_num, $next_num){
		return $this->getNotifications( $user_id, self::PUSH_ITEM_TYPE, self::NEW_STATUS, $current_num, $next_num );
	}
	
	public function getAllPushItemNotifications( $user_id, $current_num, $next_num){
		return $this->getNotifications( $user_id, self::PUSH_ITEM_TYPE, NULL, $current_num, $next_num );
	}
	
	public function getNewRelationNotifications( $user_id, $current_num, $next_num){
		return $this->getNotifications( $user_id, self::RELATION_TYPE, self::NEW_STATUS, $current_num, $next_num );
	}
	
	public function getAllRelationNotifications( $user_id, $current_num, $next_num){
		return $this->getNotifications( $user_id, self::RELATION_TYPE, NULL, $current_num, $next_num );
	}
	
	public function getNewCommentNotifications( $user_id, $current_num, $next_num){
		return $this->getNotifications( $user_id, self::COMMENT_TYPE, self::NEW_STATUS, $current_num, $next_num );
	}
	
	public function getAllCommentNotifications( $user_id, $current_num, $next_num){
		return $this->getNotifications( $user_id, self::COMMENT_TYPE, NULL, $current_num, $next_num );
	}
	
	public function getAllNewNotifications($user_id, $current_num, $next_num){
		return $this->getNotifications( $user_id, NULL, self::NEW_STATUS, $current_num, $next_num );
	}
	
	public function getAllNotifications($user_id, $current_num, $next_num){
		return $this->getNotifications( $user_id, NULL, NULL, $current_num, $next_num );
	}
	
	public function addPushItemNotification( $json ) {
		$profile_arr = json_decode( $json, true );
		
		$user_id = $profile_arr['user_id'];
		$type = self::PUSH_ITEM_TYPE;
		$content = $profile_arr['content'];
		
		$this->addNotification( $user_id, $type, $content );
	}
	
	public function addRelationNotication( $json ) {
		$profile_arr = json_decode( $json, true );
		
		$user_id = $profile_arr['user_id'];
		$type = self::RELATION_TYPE;
		$content = $profile_arr['content'];
		
		$this->addNotification( $user_id, $type, $content );
	}
	
	public function addCommentNotication( $json ) {
		$profile_arr = json_decode( $json, true );
		
		$user_id = $profile_arr['user_id'];
		$type = self::COMMENT_TYPE;
		$content = $profile_arr['content'];
		
		$this->addNotification( $user_id, $type, $content );
	}
	
	public function cancelNotification( $notification_id ){
		$status = self::READ_STATUS;
		$sql = "UPDATE NOTIFICATIONS
				SET STATUS=$status
				WHERE
					NOTIFICATIONID=$notification_id;";
		print_r($sql);
		
		$this->load->database();
		$query = $this->db->query($sql);
		$this->db->close();
	}
}